<?php
/**
 * Factory class that returns a Alinks_ImportExport_Import_Interface
 * instance
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Factory.php 391 2008-07-03 05:42:56Z sean $
 * @package Alinks_ImportExport
 */
class Alinks_ImportExport_Import_Factory
{
	/**
	 * Creates a new instance of Alinks_ImportExport_Import_Interface
	 *
	 * Given the type specified by $type, this method creates and returns
	 * a new import class instance.
	 *
	 * @param string $type The type of import object to create
	 * @param array $options Import class options
	 * @return Alinks_ImportExport_Import_Interface
	 * @throws Alinks_ImportExport_Import_Exception
	 */
	public static function factory($type, $options = array())
	{
		$type = ucwords(strtolower($type));
		$class = 'Alinks_ImportExport_Import_' . $type;
		if (!class_exists($class)) {
			$file = "Alinks/ImportExport/Import/$type.php";
			if (!Alinks_Files::exists($file)) {
				/**
				 * @see Alinks_ImportExport_Import_Exception
				 */
				require_once('Alinks/ImportExport/Import/Exception.php');
				throw new Alinks_ImportExport_Import_Exception("No import class is installed for the type $type. Searched for $file");
			}
			
			@require_once($file);
			if (!class_exists($class)) {
				/**
				 * @see Alinks_ImportExport_Import_Exception
				 */
				require_once('Alinks/ImportExport/Import/Exception.php');
				throw new Alinks_ImportExport_Import_Exception("Found $file, but not class $class");
			}
		}
		
		return new $class($options);
	}
	
	/**
	 * Returns a new instance of Alinks_ImportExport_Import_Interface
	 *
	 * Similar to the factory() method, except this method analyzes the contents
	 * of $string to determine what type of import object to return.
	 *
	 * @param string $string The import data from the uploaded document
	 * @param array $options The import class options
	 * @return Alinks_ImportExport_Import_Interface
	 * @throws Alinks_ImportExport_Import_Exception
	 */
	public static function factoryFromString($string, $options = array())
	{
		/**
		 * The alinks 2.0 XML format
		 */
		if (preg_match(&#8217;/<Export.*alinks_version=&#8221;2\\..*&#8221;>/&#8217;, $string)) {
			return self::factory('xml', $options);
			
		/**
		 * The ale documents start with a MD5 hash
		 */
		} else if (preg_match('/^[a-z0-9]{32}\\b/i', $string)) {
			return self::factory('ale', $options);
			
		/**
		 * No clue what kind of document it is
		 */
		} else {
			/**
			 * @see Alinks_ImportExport_Import_Exception
			 */
			require_once('Alinks/ImportExport/Import/Exception.php');
			throw new Alinks_ImportExport_Import_Exception("Could not determined the uploaded document type.");
		}
	}
}